﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WinnerCMS.Common
{
    /// <summary>
    /// 不重复随机数
    /// </summary>
    public class MyRandom : Random
    {
        private readonly List<int> _list = new List<int>();

        /// <summary>
        /// 返回一个有限，非负数。
        /// </summary>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public override int Next(int maxValue)
        {
            return Next(0, maxValue);
        }
        /// <summary>
        /// 返回一个有限，非负数。
        /// </summary>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public override int Next(int minValue, int maxValue)
        {
            if (minValue == maxValue)
                return minValue;

            int next = base.Next(minValue, maxValue);

            if (this._list.Count >= maxValue - minValue)
                return next;

            if (this._list.Contains(next))
                return Next(minValue, maxValue);

            this._list.Add(next);

            return next;
        }
    }

}